package com.farproc.bugreporter.test;


import com.farproc.android.BugReporter;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity implements OnClickListener, OnCheckedChangeListener, android.content.DialogInterface.OnClickListener {
    
	private Button mThrowButton, mThrowFromThreadButton;
	private CheckBox mPromptAtStartupCheckBox, mPromptWhenCaughtCheckBox;
		
	private SharedPreferences mPref;
	
	private static final int DIALOG_PROMPT = 1;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mPref = getSharedPreferences(MyApplication.PREFERENCE_NAME, Context.MODE_PRIVATE);
        
        // If you do not use MyApplication, you can call
        // BugReporter.setupMonitor()
        // here.
        
        mThrowButton = (Button)findViewById(R.id.Throw_Button);
        mThrowFromThreadButton = (Button)findViewById(R.id.Throw_From_Thread_Button);
        
        mThrowButton.setOnClickListener(this);
        mThrowFromThreadButton.setOnClickListener(this);
        
        mPromptAtStartupCheckBox = (CheckBox)findViewById(R.id.Prompt_At_Startup_CheckBox);
        mPromptWhenCaughtCheckBox = (CheckBox)findViewById(R.id.Prompt_When_Caught_CheckBox);
        
        mPromptAtStartupCheckBox.setChecked(mPref.getBoolean(MyApplication.KEY_PROMPT_AT_STARTUP, false));
        mPromptWhenCaughtCheckBox.setChecked(mPref.getBoolean(MyApplication.KEY_PROMPT_WHEN_CAUGHT, false));
        
        mPromptAtStartupCheckBox.setOnCheckedChangeListener(this);
        mPromptWhenCaughtCheckBox.setOnCheckedChangeListener(this);
        
        if(!BugReporter.isInstalled(this) && mPref.getBoolean(MyApplication.KEY_PROMPT_AT_STARTUP, false)) {
        	showDialog(DIALOG_PROMPT);
        }
    }
    
    @Override
    public Dialog onCreateDialog(int id) {
    	switch(id) {
    	case DIALOG_PROMPT:
    		return new AlertDialog.Builder(this)
    			.setTitle("Install Bug Reporter")
    			.setMessage("Bug Reporter is not installed. If you install it, you can ... Do you want to install it now?")
    			.setPositiveButton(android.R.string.yes, this)
    			.setNegativeButton(android.R.string.no, null)
    			.create();
    	}
    	return null;
    }
    
    private Handler mHandler = new Handler();

	@Override
	public void onClick(View view) {
		if(view == mThrowButton) {
			throw new RuntimeException("Throw from UI thread!");
		} else if(view == mThrowFromThreadButton) {
			final long threadId = Thread.currentThread().getId();
			final boolean promptWhenCaught = mPref.getBoolean(MyApplication.KEY_PROMPT_WHEN_CAUGHT, false);
			final Thread t = new Thread(new Runnable() {
				
				@Override
				public void run() {
					BugReporter.setupMonitor(getApplicationContext(),
							mHandler, threadId,
							"your email address", null, 
							promptWhenCaught ? PromptToInstall.class : null);
					throw new RuntimeException("Throw from another thread!");
				}
			});
			t.setName("t");
			t.start();
		}
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		final Editor edt = mPref.edit();
		if(buttonView == mPromptAtStartupCheckBox) {
			edt.putBoolean(MyApplication.KEY_PROMPT_AT_STARTUP, isChecked);
		} else if(buttonView == mPromptWhenCaughtCheckBox) {
			edt.putBoolean(MyApplication.KEY_PROMPT_WHEN_CAUGHT, isChecked);
		}
		edt.commit();
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		BugReporter.installFromMarket(this);
	}
}